package leetcode.datastructure.stack;

import java.util.Stack;

public class Program232Stack2Queue {

    public Stack<Integer> inStack;
    public Stack<Integer> outStack;

    public Program232Stack2Queue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if (outStack.empty()) {
            while (!inStack.empty()) {
                outStack.push(inStack.pop());
            }
        }

        if (outStack.empty()) {
            return -1;
        }
        return outStack.pop();
    }

    public int peek() {
        if (outStack.empty()) {
            while (!inStack.empty()) {
                outStack.push(inStack.pop());
            }
        }

        if (outStack.empty()) {
            return -1;
        }
        return outStack.peek();
    }

    public boolean empty() {
        return inStack.empty() && outStack.empty();
    }

    public static void main(String[] args) {
        Program232Stack2Queue myQueue = new Program232Stack2Queue();
        myQueue.push(1); // queue is: [1]
        myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
        System.out.println(myQueue.peek()); // return 1
        System.out.println(myQueue.pop()); // return 1, queue is [2]
        System.out.println(myQueue.empty()); // return false
    }
}
